//
//  MenuCell.swift
//  clreader
//
//  Created by colin on 2019/3/6.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

extension HomeViewController {
    
    class MenuCell: UITableViewCell {
        
        enum MenuItem : String, CaseIterable {
            case 精讲 = "精讲"
            case 有声书 = "有声书"
            case 电子书 = "电子书"
            case 视频 = "视频"
            case 分类 = "分类"
            
            var imageName:String {
                switch self {
                case .精讲: return "menu_1"
                case .有声书: return "menu_2"
                case .电子书: return "menu_3"
                case .视频: return "menu_4"
                case .分类: return "menu_5"
                }
            }
            
        }
        
        var itemHandle:((_ type:MenuItem)->Void)?
        
        static let height:CGFloat = 110.0
        static let reuseId = "MenuCell"
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.selectionStyle = .none
            
            let distance = 10.0            
            var pre:UIView?
            for index in 0 ..< MenuItem.allCases.count {
                let type = MenuItem.allCases[index]
                
                let menu = CLTouchableView().then {
                    $0.layer.cornerRadius = 5.0
                    $0.layer.masksToBounds = true
                    $0.isUserInteractionEnabled = true
                    $0.tag = index
                    let tap = UITapGestureRecognizer(target: self, action: #selector(self.itemClickAction(_:)))
                    $0.addGestureRecognizer(tap)
                    
                    let imageViewIcon = UIImageView(image: UIImage(named: type.imageName))
                    $0.addSubview(imageViewIcon)
                    imageViewIcon.snp.makeConstraints({ (make) in
                        make.size.equalTo(CGSize(width: 40.0, height: 40.0))
                        make.centerX.equalToSuperview()
                        make.top.equalToSuperview().offset(10.0)
                    })
                    
                    let labelTitle = UILabel().then {
                        $0.text = type.rawValue
                        $0.textColor = UIColor(hex: "828282")
                        $0.font = UIFont.systemFont(ofSize: 12.0, weight: .light)
                    }
                    $0.addSubview(labelTitle)
                    labelTitle.snp.makeConstraints({ (make) in
                        make.centerX.equalToSuperview()
                        make.top.equalTo(imageViewIcon.snp.bottom).offset(10.0)
                        make.bottom.equalToSuperview().offset(-10.0)
                    })
                }
                self.contentView.addSubview(menu)
                menu.snp.makeConstraints { (make) in
                    if let pre_ = pre {
                        make.left.equalTo(pre_.snp.right).offset(distance)
                        make.top.equalTo(pre_.snp.top)
                        make.width.equalTo(pre_.snp.width)
                    } else {
                        make.left.equalToSuperview().offset(distance)
                        make.top.equalToSuperview().offset(20.0)
                    }
                    if index == MenuItem.allCases.count - 1 {
                        make.right.equalToSuperview().offset(-distance)
                    }
                }
                
                pre = menu
            }
            
        }
        
        @objc private func itemClickAction(_ sender:UIGestureRecognizer?) {
            if let index = sender?.view?.tag,index < MenuItem.allCases.count {
                self.itemHandle?(MenuItem.allCases[index])
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
}
